package com.bw.controller;


import com.bw.common.resp.ResponseResult;
import com.bw.entity.Goods;
import com.bw.entity.Orders;
import com.bw.entity.group.OrderGoods;
import com.bw.service.IGoodsService;
import com.bw.service.IOrdersService;
import com.bw.utils.auth.JwtUtils;
import com.bw.utils.auth.RsaUtils;
import com.bw.utils.auth.UserInfo;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.security.PublicKey;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lizhengqian
 * @since 2020-05-29
 */
@RestController
@RequestMapping("orders")
public class OrdersController { //测试

    @Autowired
    private IOrdersService orderService;

    @Autowired
    private IGoodsService goodsService;

    @PostMapping("list")
    public ResponseResult list(@RequestBody Orders order){
        ResponseResult result=ResponseResult.SUCCESS();
        try {
            List<OrderGoods> list=orderService.getList();
            result.setResult(list);
            result.setMessage("查询成功！");
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("查询失败："+e.toString());
        }
        return result;
    }

    @PostMapping("save")
    public ResponseResult save(@RequestBody Orders orders, HttpServletRequest request){
        ResponseResult result=ResponseResult.SUCCESS();

        try{
             //根据gid获取商品对象
             Goods goods = goodsService.getById(orders.getGid());

            if(goods.getCount()<10){
                //获取登录人信息
                String token=request.getHeader("token");
                PublicKey publicKey= RsaUtils.getPublicKey("D:\\1711A\\keys\\rsa.pub");
                UserInfo userInfo = JwtUtils.getInfoFromToken(token, publicKey);

                orders.setOname(userInfo.getUsername());//设置采购人为当前登录人

                orders.setStatus(0);//订单状态默认为0【未审核】
                orders.setTime(new Date());//采购时间为当期系统时间

                orders.setDdbh("CDGG1234568951");
                orders.setGys("demo");
                orders.setGid(orders.getGid());
                orders.setNum(orders.getNum());
                orders.setMoney(orders.getMoney());

                orderService.saveOrUpdate(orders);
                result.setMessage("采购成功！");

            }else{
                result.setSuccess(false);
                result.setMessage("只能采购库存量低于10的商品！");
            }

        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("采购失败："+e.toString());
        }
        return result;
    }

    @PostMapping("shenhe/{oid}")
    public ResponseResult shenhe(@PathVariable Long oid){
        ResponseResult result = ResponseResult.SUCCESS();
        try{

            //修改状态
            orderService.shenhe(oid);
            result.setMessage("审核成功");
            //获取当前对象
            Orders orders = orderService.getById(oid);
            Integer num = orders.getNum();//获取采购量

            //获取商品对象
            Goods goods = goodsService.getById(orders.getGid());
            Integer count = goods.getCount();

            count= count+num;
            goods.setCount(count);

            boolean update = goodsService.saveOrUpdate(goods);
            if(update){
                result.setMessage("更新成功");
            }else{
                result.setSuccess(false);
                result.setMessage("更新失败");
            }

        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("审核失败"+e.toString());
        }
            return result;
    }

}
